package logic

import (
	"context"
	"errors"
	"zhongwen/models/mysql"

	"zhongwen/zhongwenrpc/internal/svc"
	"zhongwen/zhongwenrpc/zhongwenrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelAddLogic) NovelAdd(in *zhongwenrpc.NovelAddRequest) (*zhongwenrpc.NovelAddResponse, error) {
	// todo: add your logic here and delete this line

	var class mysql.Class
	mysql.DB.Debug().Where("id=?", in.ClassId).Find(&class)
	if class.ID == 0 {
		return nil, errors.New("当前分类不存在")
	}
	novel := mysql.Novel{
		Title:   in.Title,
		ClassId: in.ClassId,
		Info:    in.Info,
	}
	mysql.DB.Debug().Create(&novel)
	class.NovelNum = class.NovelNum + 1
	return &zhongwenrpc.NovelAddResponse{
		NovelId: int64(novel.ID),
	}, nil
}
